//
//  ViewController.m
//  FingerPrint
//
//  Created by 李煜 on 2023/4/24.
//

#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface ViewController ()
@property(nonatomic, weak) UIAlertController *alertVC;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self touchID];
}
- (void)touchID {

    //创建LAContext
    LAContext *context = [LAContext new];

    //这个属性是设置指纹输入失败之后的弹出框的选项
    context.localizedFallbackTitle = @"没有忘记密码";

    NSError *error = nil;
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        //        NSLog(@"支持指纹识别");
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请按home键指纹登录" reply:^(BOOL success, NSError * _Nullable error) {
            if (success) {

                dispatch_async(dispatch_get_main_queue(), ^{

                    self.alertVC = [UIAlertController alertControllerWithTitle:nil message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
                    dispatch_async(dispatch_get_main_queue(), ^{

                        [self presentViewController:self.alertVC animated:YES completion:nil];
                    });
                    [self performSelector:@selector(dismiss:) withObject:self.alertVC afterDelay:5.0];

                });

            }else{
                NSLog(@"%@",error.localizedDescription);
                switch (error.code) {
                    case LAErrorSystemCancel:
                    {
                        NSLog(@"系统取消授权，如其他APP切入");
                        break;
                    }
                    case LAErrorUserCancel:
                    {
                        NSLog(@"用户取消验证Touch ID");
                        break;
                    }
                    case LAErrorAuthenticationFailed:
                    {
                        NSLog(@"授权失败");
                        break;
                    }
                    case LAErrorPasscodeNotSet:
                    {
                        NSLog(@"系统未设置密码");
                        break;
                    }
                    case LAErrorTouchIDNotAvailable:
                    {
                        NSLog(@"设备Touch ID不可用，例如未打开");
                        break;
                    }
                    case LAErrorTouchIDNotEnrolled:
                    {
                        NSLog(@"设备Touch ID不可用，用户未录入");
                        break;
                    }
                    case LAErrorUserFallback:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"用户选择输入密码，切换主线程处理");
                        }];
                        break;
                    }
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"其他情况，切换主线程处理");
                        }];
                        break;
                    }
                }
            }
        }];
    }else{



        switch (error.code) {
            case LAErrorTouchIDNotEnrolled:
                NSLog(@"TouchID is not enrolled");
                break;
            case LAErrorPasscodeNotSet:
            {
                NSLog(@"A passcode has not been set");
                break;
            }
            default:
            {
                NSLog(@"TouchID not available");
                break;
            }
        }

        NSLog(@"%@",error.localizedDescription);
    }

}

-(void) dismiss:(UIAlertController*)alert {
    [alert dismissViewControllerAnimated:true completion:nil];
}
@end
